package com.cyy.partner.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyy.partner.common.Result;
import com.cyy.partner.pojo.Activity;
import com.cyy.partner.pojo.Records;
import com.cyy.partner.service.ActivityService;
import com.cyy.partner.service.RecordsService;
import com.cyy.partner.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/5/2
 */
@Api(tags = "活动管理相关接口")
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private RecordsService recordsService;

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @ApiOperation("添加活动接口")
    @PostMapping("/add")
    public Result<String> add(@RequestBody Activity activity){
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getActivityName, activity.getActivityName());
        Activity one = activityService.getOne(queryWrapper);
        if(one != null){
            return Result.error("该活动已有啦");
        }
       if(LocalDateTime.now().isAfter(activity.getEndTime())){
           return Result.error("截止时间不能比创建时间早哦");
       }
        activity.setCreateTime(LocalDateTime.now());
        activityService.save(activity);
        //加入记录
        Records records = new Records(activity.getUserId(), activity.getId());
        recordsService.save(records);
        return Result.success("添加活动成功");
    }

    /**
     * 活动列表
     * @param categoryId
     * @param activityName
     * @return
     */
    @ApiOperation("活动列表接口")
    @GetMapping("/list")
    public Result<List<Activity>> list(@RequestParam(required = false) Long categoryId,
                                       @RequestParam(required = false) String activityName)
    {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(activityName!=null, Activity::getActivityName, activityName);
//        System.out.println(categoryId);
        if(categoryId!=null)queryWrapper.eq(Activity::getCategoryId, categoryId);
        queryWrapper.ge(Activity::getEndTime, LocalDateTime.now());//过期的就不要了
        queryWrapper.orderByDesc(Activity::getCreateTime);
        List<Activity> list = activityService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 个人活动记录
     * @return
     */
    @ApiOperation("个人活动记录列表接口")
    @GetMapping("/listRecords")
    public Result<List<Activity>> listRecords()
    {
        HashMap<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getUserId, id);
        queryWrapper.ge(Activity::getEndTime, LocalDateTime.now());//过期的就不要了
        queryWrapper.orderByDesc(Activity::getEndTime);
        List<Activity> list = activityService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 过期活动记录
     * @return
     */
    @ApiOperation("过期活动记录接口")
    @GetMapping("/overdueRecords")
    public Result<List<Activity>> overdueRecords ()
    {
        HashMap<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getUserId, id);
        queryWrapper.lt(Activity::getEndTime, LocalDateTime.now());//过期的就不要了
        queryWrapper.orderByDesc(Activity::getEndTime);
        List<Activity> list = activityService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @ApiOperation("根据活动id删除活动接口")
    @DeleteMapping
    public Result<String> deleteRecords(Long id)
    {
        //先删除活动记录
        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Records::getActivityId, id);
        recordsService.remove(queryWrapper);
        //再删除活动
        activityService.removeById(id);
        return Result.success("删除活动成功");
    }

    //活动记录

    /**
     * 报名活动
     * @param id
     * @return
     */
    @ApiOperation("根据id报名参加活动接口")
    @PostMapping("/joinActivity")
    public Result<String> joinActivity(Long id){
        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Records::getActivityId, id);
        int cur = recordsService.list(queryWrapper).size();
        Activity activity = activityService.getById(id);
        if(activity.getNumber() == cur){
            return Result.error("该活动人数已满");
        }
        HashMap<String, Object> map = ThreadLocalUtil.get();
        Long id1 = (Long) map.get("id");
        queryWrapper.eq(Records::getUserId, id1);
        List<Records> list = recordsService.list(queryWrapper);
        if(list.size() != 0){
            return Result.error("已加入该搭子活动啦");
        }else{
            Records records = new Records(id1, id);
            recordsService.save(records);
            return Result.success("成功加入该搭子活动啦");
        }
    }

    /**
     * 加入的活动记录
     * @return
     */
    @ApiOperation("报名加入的活动记录列表接口")
    @GetMapping("joinList")
    public Result<List<Activity>> joinList(){
        HashMap<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Records::getUserId, id);//该用户的所有记录
        List<Records> list = recordsService.list(queryWrapper);
        List<Activity> res = new ArrayList<>();//记录所有活动
        for (Records e :list) {
            Activity activity = activityService.getById(e.getActivityId());
            if(activity.getEndTime().isAfter(LocalDateTime.now())){//过期的不要
                res.add(activityService.getById(e.getActivityId()));
            }
        }
        return Result.success(res);
    }

    //删除活动

    /**
     * 取消报名
     * @param id
     * @return
     */
    @ApiOperation("取消报名活动接口")
    @DeleteMapping("/cancel")
    public Result<String> activityCancel(Long id){
        HashMap<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Long) map.get("id"));
        Activity activity = activityService.getById(id);
        if(activity.getUserId().equals(userId)){//删掉自己发布的
            return Result.error("该活动为您发布，请慎重考虑后在发布页删除");
        }else{
            LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Records::getUserId, userId);
            queryWrapper.eq(Records::getActivityId, id);
            recordsService.remove(queryWrapper);
            return Result.success("取消报名成功");
        }
    }

}
